﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using XingLucifer.IBase;

namespace XingLucifer.Devices.Inits
{
    public abstract class UT66 : IDeviceInit
    {
        protected Action<string> _action;
        private XingLucifer.IBase.Enums.DeviceTypes _name;
        public XingLucifer.IBase.Enums.DeviceTypes Name { get => _name; }

        public UT66(XingLucifer.IBase.Enums.DeviceTypes name, Action<string> action)
        {
            _name = name;
            _action = action;
        }

        public virtual bool Init(string? sourceIP, string? ip, string? dns, int port)
        {
            return true;
        }

        public virtual bool Ping(string? ip = null)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();
            options.DontFragment = true;
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            PingReply? reply = null;
            for (int i = 0; i < 3; i++)
            {
                if (reply != null) { break; }
                reply = pingSender.Send(ip, 3000, buffer, options);
                System.Threading.Thread.Sleep(1000);
            }
            return reply.Status == IPStatus.Success;
        }

        public virtual bool Reboot()
        {
            bool bl = RefererRoot("192.168.1.125");
            System.Threading.Thread.Sleep(5000);
            return bl;
        }
        protected bool SetIP(string set_ip, string ip)
        {
            string? str = GetHtml(
                $"http://{ip}/0100?0=UT-6604&1={set_ip}&2=255.255.255.0&3=192.168.1.1&4=0.0.0.0&5=0.0.0.0&6=0&7=0&E=%C8%B7%B6%A8",
                ip,
                $"http://{ip}/0100");
            return str?.Contains("DOCTYPE HTML PUBLIC") ?? false;
        }

        protected bool RefererRoot(string host)
        {
            string? str = GetHtml(
                    $"http://{host}/0800?E=%C8%B7%B6%A8",
                    host,
                    $"http://{host}/0800");
            return str?.Contains("DOCTYPE HTML PUBLIC") ?? false;
        }
        protected string GetHtml(string url, string host, string referer)
        {
            HttpWebRequest request = WebRequest.CreateHttp(url);
            request.Method = "GET";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50";
            request.Host = host;
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            request.Referer = referer;
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
            request.KeepAlive = true;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            byte[] byte1 = new byte[2048];
            int length = 0;
            using (Stream stream = response.GetResponseStream())
            {
                length = stream.Read(byte1, 0, 2048);
            }
            return System.Text.Encoding.UTF8.GetString(byte1.Take(length).ToArray());
        }
    }
}
